Project Valhalla (Java language)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
top
The project pagecite-ref-2[2] offers an overview as well as links to technical drafts and early-access builds.
Contents
• See also
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Planned features
• Value Classes and Objects: highly-efficient objects without their own identity (reference value).
• Null-restricted and Nullable types, and Null-restricted Objects: for example, using ? or ! after type declaration to say if null is allowed or not.
• Enhanced Primitive Boxing: for example, to allow code such as List<int>.
• Reified Generics: retaining actual type at runtime.
These features will require both syntax and VM-level changes.
Project components
The project is organized into several JEPs (JDK Enhancement Proposals):
• JEP draft: Value Objects (Withdrawn)cite-ref-5[5]
• JEP 401: Primitive Classes (Preview)cite-ref-6[6]
• JEP 402: Enhanced Primitive Boxing (Preview)cite-ref-7[7]
• JEP draft: Universal Generics (Withdrawn)cite-ref-8[8]
• JEP draft: Null-Restricted Value Class Types (Preview)cite-ref-9[9]
Value classes
Value classes are reference types, in the same way as all existing Java classes. However, they give up the ability to have identity. This means that the == operator compares instance of the value class by equality of their components, instead of by identity. Additionally, synchronizing on instances of value classes will fail.
Value classes still support null, since they are reference types. The Java Virtual Machine is expected to be able to take advantage of the additional constraints of value classes to eliminate heap allocation of value types in the vast majority of cases. However, storing instances of value classes into a field or upcasting them to an interface will still require an allocation.
Existing types in the Java API such as java.util.Optional are known as value-based classes, and are candidates for being made into value classes in a future JDK release.
Primitive classes
Primitive classes are subject to all the constraints of value classes, but are not reference types. This means they give up the ability to support null. Instead, their default values are the zero value for each of the component types (0 for numerical types, false for booleans, null for reference types, and the zero value for nested primitive classes).
All primitive classes are stored "inline", that is, without requiring a heap allocation. Arrays of primitive classes will not require a pointer indirection from the array to the heap. Where needed, conversions will be inserted to "box" the primitive class into a value class version of itself and vice versa.
Classes for the basic primitives
This JEP is meant to express the classical primitive types of the Java Virtual Machine (byte, char, short, int, long, boolean, float, double) as primitive classes.
Traditionally, the eight primitive types are treated separately from all other types. Providing primitive class declarations from them removes much of this special-casing, leading to a more elegant and easy to understand type system.
Technical benefits and implications
Memory access performance and the efficiency of 'boxed' value access are a major area to be addressed by these features. 'Value Type' features and 'Generic specialization' (when applied to lists or collections) reduce memory usage, but more importantly avoid pointer indirection which typically causes a cache miss.cite-ref-10[10]cite-ref-literatejava-11-0[11]
Instead of a list or array of object references, pointing to data values scattered throughout memory, Project Valhalla enhancements will enable list or array values to potentially be laid out linearly—without indirection—as a consecutive block of memory.
Value Types are envisaged as "Codes like a class, works like an int!"cite-ref-value-types-proposal-12-0[12] Synchronization and inheritance would be excluded for Value Types. These would no longer require object identity and associated memory/ pointer overheads, though would be able to assume a 'boxed' form for compatibility.cite-ref-literatejava-11-1[11]
See also
References
cite-note-11. ↑ citerefgoetzGoetz, Brian. "Welcome to Valhalla!". OpenJDK mail archive. OpenJDK. Retrieved 12 August 2014.
cite-note-22. ↑ https://openjdk.org/projects/valhalla/
cite-note-33. ↑ citerefevansEvans, Ben. "Oracle Launches Project Valhalla for Java". InfoQ. InfoWorld. Retrieved 12 August 2014.
cite-note-44. ↑ https://openjdk.org/projects/valhalla
cite-note-55. ↑ https://openjdk.org/jeps/8277163
cite-note-66. ↑ https://openjdk.org/jeps/401
cite-note-77. ↑ https://openjdk.org/jeps/402
cite-note-88. ↑ https://openjdk.org/jeps/8261529
cite-note-99. ↑ https://openjdk.org/jeps/8316779
cite-note-1010. ↑ citerefkrillKrill, Paul. "Next generation Project Valhalla proposed; Value types, generic specialization, and enhanced volatiles top the wish-list for Project Valhalla". JavaWorld. InfoWorld. Retrieved 12 August 2014.
cite-note-literatejava-1111. ↑ "Value Types & List<int> coming for Java 10 ?". LiterateJava.com. Retrieved 12 August 2014.
cite-note-value-types-proposal-1212. ↑ citerefrosegoetzsteeleRose, John; Goetz, Brian; Steele, Guy. "State of the Values". OpenJDK. Retrieved 12 August 2014.
External links
• Java incubator to explore technologies for Java 10 and beyond - JavaWorld
• Value Types & List<int> coming for Java 10? - LiterateJava.com
• OpenJDK - Project Valhalla